package com.uuah.server.context.notification;

import com.uuah.server.api.IUuahContext;
import com.uuah.server.api.context.notification.IBlockingServerEvent;

/**
 * <code>UuahContextNotification</code> is fired when an event such as the uuah
 * context starting occurs. The payload of this event will always be a reference
 * to the uuahContext.
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午01:34:43
 * @version 1.0.0
 */
public class UuahContextNotification extends ServerNotification implements
		IBlockingServerEvent {
	private static final long serialVersionUID = -3246036188011581121L;

	public static final int CONTEXT_INITIALISING = CONTEXT_EVENT_ACTION_START_RANGE + 1;
	public static final int CONTEXT_INITIALISED = CONTEXT_EVENT_ACTION_START_RANGE + 2;
	public static final int CONTEXT_STARTING = CONTEXT_EVENT_ACTION_START_RANGE + 3;
	public static final int CONTEXT_STARTED = CONTEXT_EVENT_ACTION_START_RANGE + 4;
	public static final int CONTEXT_STOPPING = CONTEXT_EVENT_ACTION_START_RANGE + 5;
	public static final int CONTEXT_STOPPED = CONTEXT_EVENT_ACTION_START_RANGE + 6;
	public static final int CONTEXT_DISPOSING = CONTEXT_EVENT_ACTION_START_RANGE + 7;
	public static final int CONTEXT_DISPOSED = CONTEXT_EVENT_ACTION_START_RANGE + 8;

	static {
		registerAction("uuah App Server context initialising", CONTEXT_INITIALISING);
		registerAction("uuah App Server context initialised", CONTEXT_INITIALISED);
		registerAction("uuah App Server context starting", CONTEXT_STARTING);
		registerAction("uuah App Server context started", CONTEXT_STARTED);
		registerAction("uuah App Server context stopping", CONTEXT_STOPPING);
		registerAction("uuah App Server context stopped", CONTEXT_STOPPED);
		registerAction("uuah App Server context disposing", CONTEXT_DISPOSING);
		registerAction("uuah App Server context disposed", CONTEXT_DISPOSED);
	}

	private String domain;

	public UuahContextNotification(IUuahContext context, String action) {
		this(context, getActionId(action));
	}

	public UuahContextNotification(IUuahContext context, int action) {
		super(getId(context), action);
		resourceIdentifier = getId(context);
		this.domain = context.getConfiguration().getDomainId();
	}

	private static String getId(IUuahContext context) {
		return context.getConfiguration().getDomainId() + "."
				+ context.getConfiguration().getId();
	}

	public String getDomain() {
		return domain;
	}

	protected String getPayloadToString() {
		return ((IUuahContext) source).getConfiguration().getId();
	}

	public String toString() {
		return EVENT_NAME + "{" + "action=" + getActionName(action)
				+ ", resourceId=" + resourceIdentifier + ", timestamp="
				+ timestamp + "}";
	}

}
